package com.lga.dataStructure;

/**
 *describe: 数组队列（顺序队列）
 * 
 *@author lga 
 *@date  2019/12/8 0008 19:47
 */ 

public class ArrayQueue {

    private int size = 0;
    private int capacity;
    private int head = 0;
    private int tail = 0;
    private Object[] datas;

    public ArrayQueue(int capacity) {
        this.capacity = capacity;
        datas = new Object[capacity];
    }

    /**
     * 入队操作
     * @param data
     * @return
     */
    public boolean push(Object data) {
        if (capacity <= tail) {
            if (head == 0) {
                System.out.println("队列已满，无法插入数据");
                return false;
            }
            //迁移数据
            for (int i = head; i < tail; i++) {
                datas[i - head] = datas[i];
            }
            tail = tail - head;
            head = 0;
        }
        datas[tail++] = data;
        size++;
        return true;
    }

    /**
     * 出队操作
     * @param data
     * @return
     */
    public Object pop() {
        Object result = new Object();
        if (head == tail) {
            return false;
        }
        result = datas[head++];
        size--;
        return result;
    }

    /**
     * 获取队列的实际数据数量
     * @return
     */
    public int getSize() {
        return this.size;
    }

    public void printQueue() {
        System.out.println("开始打印队列数据");
        if (tail - head == 0) {
            return;
        }
        for (int i = head; i < tail; i++) {
            System.out.print(datas[i] +" ");
        }
    }

    public static void main(String[] args) {

        ArrayQueue arrayQueue = new ArrayQueue(3);
        arrayQueue.printQueue();
        arrayQueue.push(1);
        arrayQueue.push(2);
        arrayQueue.push(3);
        Object pop = arrayQueue.pop();
        System.out.println("pop = " + pop);
        arrayQueue.printQueue();
        int size = arrayQueue.getSize();
        System.out.println("size = " + size);
        arrayQueue.push(4);
        arrayQueue.printQueue();
        System.out.println("size = " + arrayQueue.getSize());

        arrayQueue.push(5);
        arrayQueue.printQueue();


    }
}
